﻿@using viBook.Common
@using viBook.Helpers
@using viBook.Models
@model User
@{
    var totalBookPage = (int)Math.Ceiling((double)Model.UploadedBooks.Count() / Constant.MaximunItemsToPaging);
    totalBookPage = totalBookPage == 0 ? 1 : totalBookPage;
    bool isShowNextButton = totalBookPage > 1;
}
<script type="text/javascript">
 


var books = @CustomHelpers.BooksToJavaScriptConverter(Model.UploadedBooks.Take(Constant.MaximunItemsToPaging).ToList()) ,
    viewmodel = {
        totalBookPage: ko.observable( @totalBookPage ),
        isShowPrevBtn: ko.observable(false),
        isShowNextBtn: ko.observable( @isShowNextButton.ToString().ToLower() ),
        numberOfBook: ko.observable( @Model.UploadedBooks.Count() ),
        bookPageNo : ko.observable(1),
        uploadedBooks: ko.observableArray(books),
        isReported: ko.observable(@Model.IsReported.BooleanToJavascript()),
        isBlocked: ko.observable(@Model.IsBlocked.BooleanToJavascript()),
        fetchNextCommentPage: function() {
            $.ajax({
                    url: '@Url.Action("FetchNextBookPage", "Account")',
                    type: 'POST',
                    data: ko.toJSON({ userId: @Model.Id, pageNumber: viewmodel.bookPageNo() + 1 }),
                    contentType: 'APPLICATION/JSON',
                    success: function(result) {
                        if (result.isSuccess) {
                            var listBooks = [];
                            $(result.books).each(function(index, item) {
                                 if((index+1)%3!=0)
                                listBooks[index] = new Book(item.description,item.id, item.title, item.coverImage, item.numberOfPages, item.numberOfChapters, item.numberOfLikes, item.numberOfViews, item.numberOfComments,item.uploaderImage,item.uploaderName,item.uploadedBy,item.uploadedDateString,'group') ;
                                else {
                                    listBooks[index] = new Book(item.description,item.id, item.title, item.coverImage, item.numberOfPages, item.numberOfChapters, item.numberOfLikes, item.numberOfViews, item.numberOfComments,item.uploaderImage,item.uploaderName,item.uploadedBy,item.uploadedDateString,'group third') ;
                                }  });
                            viewmodel.uploadedBooks(listBooks);
                            viewmodel.bookPageNo(viewmodel.bookPageNo() + 1);
                            viewmodel.isShowPrevBtn(viewmodel.bookPageNo() > 1);
                            viewmodel.isShowNextBtn(viewmodel.bookPageNo() < viewmodel.totalBookPage());
                        } else {
                            alert(result.message);
                        }
                    }
                });
        },
        fetchPreviousCommentPage: function() {
            $.ajax({
                    url: '@Url.Action("FetchNextBookPage", "Account")',
                    type: 'POST',
                    data: ko.toJSON({ userId: @Model.Id, pageNumber: viewmodel.bookPageNo() - 1 }),
                    contentType: 'APPLICATION/JSON',
                    success: function(result) {
                        if (result.isSuccess) {
                            var listBooks = [];
                            $(result.books).each(function(index, item) {
                                  if((index+1)%3!=0)
                                listBooks[index] = new Book(item.description,item.id, item.title, item.coverImage, item.numberOfPages, item.numberOfChapters, item.numberOfLikes, item.numberOfViews, item.numberOfComments,item.uploaderImage,item.uploaderName,item.uploadedBy,item.uploadedDateString,'group') ;
                                else {
                                    listBooks[index] = new Book(item.description,item.id, item.title, item.coverImage, item.numberOfPages, item.numberOfChapters, item.numberOfLikes, item.numberOfViews, item.numberOfComments,item.uploaderImage,item.uploaderName,item.uploadedBy,item.uploadedDateString,'group third') ;
                                }  });
                            viewmodel.uploadedBooks(listBooks);
                            viewmodel.bookPageNo(viewmodel.bookPageNo() - 1);
                            viewmodel.isShowPrevBtn(viewmodel.bookPageNo() > 1);
                            viewmodel.isShowNextBtn(viewmodel.bookPageNo() < viewmodel.totalBookPage());
                        } else {
                            alert(result.message);
                        }
                    }
                });
        },
        report: function() {
            var answer = confirm("Do you want to report this user?");
            if (answer) {
                $.ajax({
                        url: '@Url.Action("ReportUser", "Account")',
                        type: 'POST',
                        data: ko.toJSON({ id: @Model.Id }),
                        contentType: 'APPLICATION/JSON',
                        success: function(result) {
                            if (result.success) {
                                viewmodel.isReported(true);
                            }
                        }
                    });
            }
        }
    };
ko.applyBindings(viewmodel);
</script>

